package com.lxu.util;

import com.lxu.entity.WordEntity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class XWPFDocumentTextUtils {
    /**
     * 替换段落文本
     * @param document docx解析对象
     * @param wordEntity 需要替换的信息集合
     */
    public static void changeText(XWPFDocument document,WordEntity wordEntity){
        //获取段落集合
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (XWPFParagraph paragraph : paragraphs) {
            //判断此段落时候需要进行替换
            String text = paragraph.getText();
            if(XWPFDocumentBaseUtils.checkText(text)){
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    //替换模板原来位置
                    run.setText(XWPFDocumentBaseUtils.changeValue(run.toString(), wordEntity.getTextMap()),0);
                }
            }
        }
    }
}
